<?php

namespace app\controllers;

use app\models\CityForm;
use Yii;
use yii\web\Controller;


class CityController extends Controller
{

    public $enableCsrfValidation = false;

    const TOP = 'top';

    const CITYS = 'citys';

    const PAGE_SIZE = 5;


    private function showJson($code, $msg, $data = []){

        return json_encode(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

    private function initRedis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        return $redis;
    }

    public function actionCreate(){

        $form  = new CityForm();
        $form->setAttributes(Yii::$app->request->post());

        if(!$form->validate()){

            return $this->showJson(1000, '请求失败',$form->getErrors());
        }


        $redis = $this->initRedis();

        $now = time();
        $result = [
            'name' => $form->name,
            'lnt' => $form->lnt,
            'lat' => $form->lat,
            'time' => date('Y-m-d H:i:s',$now)
        ];
        $redis->zAdd(self::TOP, $now, json_encode($result));

        $redis->rawCommand('geoadd', self::CITYS, $form->lnt, $form->lat, $form->name);


        return $this->showJson(0, '请求成功');
    }


    public function actionTop(){

        $redis = $this->initRedis();

        $res = $redis->zRevRange(self::TOP,0,9);

        $result = [];
        foreach ($res as $value){
            $result[] = json_decode($value,true);
        }

        return $this->showJson(0,'请求成功',$result);

    }


    public function actionNear(){

        $name = Yii::$app->request->get('name','beijing');
        $page = Yii::$app->request->get('page',1);


        $redis = $this->initRedis();

        $res = $redis->rawCommand('georadiusbymember', self::CITYS, $name, '100', 'km', 'WITHDIST');


        $start=($page-1)*self::PAGE_SIZE;//偏移量，当前页-1乘以每页显示条数
        $pageRes = array_slice($res,$start,self::PAGE_SIZE);

        $dataList = [];

        foreach ($pageRes as $value){

            $dataList[] = [
                'name' => $value[0],
                'near' => $value[1]
            ];
        }


        $result = [
            'page_total' => ceil(count($res)/self::PAGE_SIZE),
            'data' => $dataList
        ];
        return $this->showJson(0,'请求成功',$result);
    }


    public function actionMap(){

        $name = Yii::$app->request->get('name');

        $result = [];
        $redis = $this->initRedis();
        if(empty($name)){

            $res = $redis->zRevRange(self::TOP,0,4);

            foreach ($res as $value){
                $result[] = json_decode($value,true);
            }
        }else{

            $res = $redis->rawCommand('georadiusbymember', self::CITYS, $name, '3', 'km', 'WITHCOORD','count','5');

            foreach ($res as $value){
                $result[] = [
                    'name' => $value[0],
                    'lnt' => $value[1][0],
                    'lat' => $value[1][1]
                ];
            }
        }

        return $this->render('map',['data' => json_encode($result)]);
    }
    
    public function actionGeo(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);

        $redis->rawCommand('geoadd', 'citys', '116.40', '39.90', 'beijing');
        $redis->rawCommand('geoadd', 'citys', '121.47', '31.23', 'shanghai');
        $redis->rawCommand('geoadd', 'citys', '114.30', '30.60', 'wuhan');

        $redis->rawCommand('geoadd', 'citys', '116.40', '39.90', 'beijing');
        $redis->rawCommand('geoadd', 'citys', '121.47', '31.23', 'shanghai');
        $redis->rawCommand('geoadd', 'citys', '114.30', '30.60', 'wuhan');

        echo '<pre>';

//获取两个地理位置的距离，单位：m(米，默认)， km(千米)， mi(英里)， ft(英尺)
        var_dump($redis->rawCommand('geodist', 'citys', 'beijing', 'wuhan'));
        var_dump($redis->rawCommand('geodist', 'citys', 'beijing', 'shanghai', 'km'));

//获取成员的经纬度
        var_dump($redis->rawCommand('geopos', 'citys', 'shanghai'));

//获取成员的经纬度hash，geohash表示坐标的一种方法，便于检索和存储
        var_dump($redis->rawCommand('geohash', 'citys', 'shanghai', 'wuhan'));

//基于经纬度坐标的范围查询
//查询以经纬度为114，30为圆心，100千米范围内的成员
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km'));

//WITHCOORD表示获取成员经纬度
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'WITHCOORD'));

//WITHDIST表示获取到圆心的距离
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'WITHDIST'));

//WITHHASH表示获取成员经纬度HASH值
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'WITHHASH'));

//COUNT 数量，表示限制获取成员的数量
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'COUNT', '3'));

// ASC 根据圆心位置，从近到远的返回元素
// DESC 根据圆心位置，从远到近的返回元素
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'ASC'));

//基于成员位置范围查询
//查询以武汉为圆心，100千米范围内的成员
        var_dump($redis->rawCommand('georadiusbymember', 'citys', 'wuhan', '100', 'km'));
    }
}
